package com.iknow.view.widget.media;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import com.iknow.util.LogUtil;
import com.iknow.util.StringUtil;
import java.io.BufferedReader;
import java.io.PrintStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Lyric
{
  private static final int loopTime = 1000;
  private static final Pattern pattern = Pattern.compile("(?<=\\[).*?(?=\\])");
  private IKLyricController controller;
  private Spannable defultLrcText = null;
  private boolean isReady;
  private List<SentenceInfo> list;
  private Spannable lrcText = null;
  private Timer lrcTime;
  private TextView lrcView = null;
  private int offset;
  private View.OnLongClickListener selectLongClickListener;
  private View.OnTouchListener selectTouchListener;
  private int tempIndex;
  private int touchOffset;
  private Handler viewHandler;

  public Lyric(String paramString)
  {
    this.list = new ArrayList();
    this.controller = null;
    this.viewHandler = null;
    this.tempIndex = -1;
    this.lrcTime = null;
    this.touchOffset = 0;
    this.isReady = false;
    this.selectTouchListener = new View.OnTouchListener(this)
    {
      public boolean onTouch(, MotionEvent paramMotionEvent)
      {
        if (paramView instanceof TextView)
          switch (paramMotionEvent.getAction())
          {
          default:
          case 0:
          }
        while (true)
        {
          Layout localLayout;
          do
          {
            return false;
            localLayout = ((TextView)paramView).getLayout();
          }
          while ((localLayout == null) || (paramMotionEvent == null));
          int i = localLayout.getLineForVertical((int)paramMotionEvent.getY());
          Lyric.access$0(this.this$0, localLayout.getOffsetForHorizontal(i, (int)paramMotionEvent.getX()));
        }
      }
    };
    this.selectLongClickListener = new View.OnLongClickListener(this)
    {
      public boolean onLongClick()
      {
        long l;
        if (paramView instanceof TextView)
        {
          int i = this.this$0.getNowSentenceIndex(Lyric.access$1(this.this$0));
          if ((((!(Lyric.access$2(this.this$0).isPlay())) || (Lyric.access$3(this.this$0) != i))) && (i >= 0))
          {
            l = ((SentenceInfo)Lyric.access$4(this.this$0).get(i)).getFromTime();
            if (!(Lyric.access$2(this.this$0).isPlay()))
              break label102;
            Lyric.access$2(this.this$0).moveTo(l);
          }
        }
        while (true)
        {
          return false;
          label102: Lyric.access$2(this.this$0).play(l);
        }
      }
    };
    init(paramString);
  }

  private void init(String paramString)
  {
    StringBuffer localStringBuffer;
    int i;
    int j;
    int k;
    int l;
    if (StringUtil.isEmpty(paramString))
      return;
    try
    {
      String str3;
      while (true)
      {
        String str2;
        do
        {
          StringReader localStringReader = new StringReader(paramString);
          BufferedReader localBufferedReader = new BufferedReader(localStringReader);
          str3 = localBufferedReader.readLine();
          if (str3 != null)
            break label162;
          localBufferedReader.close();
          List localList = this.list;
          3 local3 = new Comparator(this)
          {
            public int compare(, SentenceInfo paramSentenceInfo2)
            {
              return (int)(paramSentenceInfo1.getFromTime() - paramSentenceInfo2.getFromTime());
            }
          };
          Collections.sort(localList, local3);
          k = this.list.size();
          l = 0;
          if (l < k)
            break label186;
          localStringBuffer = new StringBuffer();
          i = 0;
          if (i < this.list.size())
            break label255;
          str2 = localStringBuffer.toString();
          if (!(StringUtil.isEmpty(str2)))
            break label328;
          j = 0;
          this.isReady = j;
        }
        while (!(this.isReady));
        this.defultLrcText = new SpannableStringBuilder(str2);
        refresh();
      }
      label162: label186: label328: label255: parseLine(str3.trim());
    }
    catch (Exception localException)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
              LogUtil.e(Lyric.class, localException);
            SentenceInfo localSentenceInfo2 = null;
            if (l + 1 < k)
              localSentenceInfo2 = (SentenceInfo)this.list.get(l + 1);
            SentenceInfo localSentenceInfo3 = (SentenceInfo)this.list.get(l);
            if (localSentenceInfo2 != null)
              localSentenceInfo3.setToTime(localSentenceInfo2.getFromTime() - 1L);
            ++l;
          }
          SentenceInfo localSentenceInfo1 = (SentenceInfo)this.list.get(i);
          localSentenceInfo1.setFromIndex(localStringBuffer.length());
          String str1 = localSentenceInfo1.getContent();
          localStringBuffer.append("\n");
          localStringBuffer.append(str1);
          localStringBuffer.append("\n");
          localSentenceInfo1.setToIndex(localStringBuffer.length());
          ++i;
        }
        j = 1;
      }
    }
  }

  private void parseLine(String paramString)
  {
    label9: Matcher localMatcher;
    ArrayList localArrayList;
    int i;
    int j;
    int i1;
    String str3;
    if (paramString.equals(""));
    do
    {
      return;
      localMatcher = pattern.matcher(paramString);
      localArrayList = new ArrayList();
      i = -1;
      j = -1;
      if (localMatcher.find())
        break label155;
    }
    while (localArrayList.isEmpty());
    int l = i + j + 2;
    try
    {
      if (l > paramString.length())
      {
        int i2;
        i1 = paramString.length();
        str3 = paramString.substring(i1);
        if ((!(str3.equals(""))) || (this.offset != 0))
          break label336;
        Iterator localIterator3 = localArrayList.iterator();
        do
        {
          if (!(localIterator3.hasNext()));
          i2 = parseOffset((String)localIterator3.next());
        }
        while (i2 == 2147483647);
        this.offset = i2;
      }
    }
    catch (Exception localException)
    {
      while (true)
      {
        label155: String str2;
        Iterator localIterator1;
        break label9:
        String str1 = localMatcher.group();
        int k = paramString.indexOf("[" + str1 + "]");
        if ((i != -1) && (k - i > j + 2))
        {
          str2 = paramString.substring(2 + i + j, k);
          localIterator1 = localArrayList.iterator();
        }
        while (true)
        {
          long l1;
          do
          {
            while (!(localIterator1.hasNext()))
            {
              localArrayList.clear();
              localArrayList.add(str1);
              i = k;
              j = str1.length();
            }
            l1 = parseTime((String)localIterator1.next());
          }
          while (l1 == -1L);
          List localList1 = this.list;
          SentenceInfo localSentenceInfo1 = new SentenceInfo(str2, l1);
          localList1.add(localSentenceInfo1);
        }
        i1 = l;
      }
      label336: Iterator localIterator2 = localArrayList.iterator();
      while (true)
      {
        long l2;
        do
        {
          if (!(localIterator2.hasNext()));
          l2 = parseTime((String)localIterator2.next());
        }
        while (l2 == -1L);
        List localList2 = this.list;
        SentenceInfo localSentenceInfo2 = new SentenceInfo(str3, l2);
        localList2.add(localSentenceInfo2);
      }
    }
  }

  private int parseOffset(String paramString)
  {
    int i;
    String[] arrayOfString = paramString.split("\\:");
    if (arrayOfString.length == 2)
      if (arrayOfString[0].equalsIgnoreCase("offset"))
      {
        int j = Integer.parseInt(arrayOfString[1]);
        System.err.println("整体的偏移量：" + j);
        i = j;
      }
    while (true)
    {
      while (true)
      {
        return i;
        i = 2147483647;
      }
      i = 2147483647;
    }
  }

  private long parseTime(String paramString)
  {
    long l;
    int i1;
    int i2;
    String[] arrayOfString = paramString.split("\\:|\\.");
    if (arrayOfString.length < 2)
    {
      l = -1L;
      return l;
    }
    if (arrayOfString.length == 2);
    try
    {
      while ((this.offset == 0) && (arrayOfString[0].equalsIgnoreCase("offset")))
      {
        this.offset = Integer.parseInt(arrayOfString[1]);
        System.err.println("整体的偏移量：" + this.offset);
        l = -1L;
      }
      i1 = Integer.parseInt(arrayOfString[0]);
      i2 = Integer.parseInt(arrayOfString[1]);
      if ((i1 < 0) || (i2 < 0) || (i2 >= 60))
        throw new RuntimeException("数字不合法!");
    }
    catch (Exception localException2)
    {
      int i;
      int j;
      int k;
      while (true)
      {
        while (true)
          l = -1L;
        l = 1000L * (i2 + i1 * 60);
      }
      if (arrayOfString.length == 3);
      try
      {
        i = Integer.parseInt(arrayOfString[0]);
        j = Integer.parseInt(arrayOfString[1]);
        k = Integer.parseInt(arrayOfString[2]);
        if ((i < 0) || (j < 0) || (j >= 60) || (k < 0) || (k > 99))
          throw new RuntimeException("数字不合法!");
      }
      catch (Exception localException1)
      {
        while (true)
        {
          while (true)
            l = -1L;
          l = 1000L * (j + i * 60) + k * 10;
        }
        l = -1L;
      }
    }
  }

  private void toDefultLrcTextSpan()
  {
    this.lrcText = null;
  }

  public void adjustTime(int paramInt)
  {
    this.offset = (paramInt + this.offset);
  }

  public Spannable getLrcText()
  {
    ForegroundColorSpan[] arrayOfForegroundColorSpan;
    int j;
    int k;
    int i = null.length();
    if (this.lrcText != null)
    {
      arrayOfForegroundColorSpan = (ForegroundColorSpan[])this.lrcText.getSpans(0, this.lrcText.length(), ForegroundColorSpan.class);
      j = arrayOfForegroundColorSpan.length;
      k = 0;
    }
    while (true)
    {
      if (k >= j)
        return null;
      ForegroundColorSpan localForegroundColorSpan = arrayOfForegroundColorSpan[k];
      int l = this.lrcText.getSpanStart(localForegroundColorSpan);
      int i1 = this.lrcText.getSpanEnd(localForegroundColorSpan);
      int i2 = this.lrcText.getSpanFlags(localForegroundColorSpan);
      if ((l > 0) || (i1 < i))
        null.setSpan(localForegroundColorSpan, l, i1, i2);
      ++k;
    }
  }

  public int getNowSentenceIndex(int paramInt)
  {
    int j;
    int i = 0;
    if (i >= this.list.size())
    {
      if (this.list.size() <= 0)
        break label97;
      if (paramInt >= ((SentenceInfo)this.list.get(0)).getFromIndex())
        break label82;
      j = 0;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return j;
            if (!(((SentenceInfo)this.list.get(i)).isInIndex(paramInt)))
              break;
            j = i;
          }
          ++i;
        }
        label82: j = this.list.size() - 1;
      }
      label97: j = 0;
    }
  }

  public int getNowSentenceIndex(long paramLong)
  {
    int j;
    int i = 0;
    if (i >= this.list.size())
    {
      if (this.list.size() <= 0)
        break label102;
      if (paramLong >= ((SentenceInfo)this.list.get(0)).getFromTime())
        break label86;
      j = 0;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            return j;
            if (!(((SentenceInfo)this.list.get(i)).isInTime(paramLong)))
              break;
            j = i;
          }
          ++i;
        }
        label86: j = this.list.size() - 1;
      }
      label102: j = 0;
    }
  }

  public void play()
  {
    stop();
    if (!(this.isReady));
    while (true)
    {
      return;
      if (this.lrcTime == null)
        this.lrcTime = new Timer();
      this.lrcTime.schedule(new TimerTask(this)
      {
        public void run()
        {
          try
          {
            if (!(Lyric.access$2(this.this$0).isPlay()))
            {
              this.this$0.stop();
            }
            else
            {
              int i = this.this$0.getNowSentenceIndex(Lyric.access$2(this.this$0).getCurrentPosition());
              Lyric.access$8(this.this$0, i);
              Message localMessage = Lyric.access$9(this.this$0).obtainMessage(0);
              Bundle localBundle = new Bundle();
              SentenceInfo localSentenceInfo = (SentenceInfo)Lyric.access$4(this.this$0).get(i);
              localBundle.putInt("LyricViewHandler.PARM_TEXT_FORM", localSentenceInfo.getFromIndex());
              localBundle.putInt("LyricViewHandler.PARM_TEXT_TO", localSentenceInfo.getToIndex());
              localMessage.setData(localBundle);
              localMessage.sendToTarget();
            }
          }
          catch (Throwable localThrowable)
          {
            LogUtil.e(getClass(), localThrowable);
          }
        }
      }
      , 0L, 1000L);
    }
  }

  public void refresh()
  {
    toDefultLrcTextSpan();
    if (this.lrcView != null)
      this.lrcView.setText(this.lrcText);
  }

  public void registerLrcView(View paramView, IKLyricController paramIKLyricController)
  {
    if (!(this.isReady));
    while (true)
    {
      while (true)
      {
        return;
        if ((paramView != null) && (paramIKLyricController != null))
          break;
        this.isReady = false;
      }
      this.viewHandler = new LyricViewHandler(this);
      this.lrcView = ((TextView)paramView.findViewById(2131296263));
      if (this.lrcView == null)
        throw new RuntimeException("Can Not Found R.id.lyric_view");
      refresh();
      this.controller = paramIKLyricController;
      paramView.setOnTouchListener(this.selectTouchListener);
      paramView.setOnLongClickListener(this.selectLongClickListener);
    }
  }

  public void setLrcText(Spannable paramSpannable)
  {
    this.lrcText = paramSpannable;
    if (this.lrcView != null)
      this.lrcView.setText(paramSpannable);
  }

  public void stop()
  {
    if (!(this.isReady));
    while (true)
    {
      return;
      if (this.lrcTime != null)
      {
        this.lrcTime.cancel();
        this.lrcTime = null;
      }
      refresh();
    }
  }

  public static abstract interface IKLyricController
  {
    public abstract long getCurrentPosition();

    public abstract boolean isPlay();

    public abstract void moveTo(long paramLong);

    public abstract void play(long paramLong);
  }

  private static class LyricViewHandler extends Handler
  {
    public static final String PARM_TEXT_FORM = "LyricViewHandler.PARM_TEXT_FORM";
    public static final String PARM_TEXT_TO = "LyricViewHandler.PARM_TEXT_TO";
    public static final int WHAT_DRAW_PLAYCOLOR;
    private Lyric lyric = null;

    public LyricViewHandler(Lyric paramLyric)
    {
      this.lyric = paramLyric;
    }

    public void handleMessage(Message paramMessage)
    {
      Lyric.access$5(this.lyric);
      Bundle localBundle = paramMessage.getData();
      int i = localBundle.getInt("LyricViewHandler.PARM_TEXT_FORM");
      int j = localBundle.getInt("LyricViewHandler.PARM_TEXT_TO");
      if (Lyric.access$2(this.lyric).isPlay())
        switch (paramMessage.what)
        {
        default:
        case 0:
        }
      while (true)
      {
        Lyric.access$7(this.lyric).setText(Lyric.access$6(this.lyric));
        return;
        Lyric.access$6(this.lyric).setSpan(new ForegroundColorSpan(-65536), i, j, 33);
      }
    }
  }
}